import {defineStore} from 'pinia'
import axios from 'axios'
interface ShopcartItem{
    id:number
    pname:string
    price:number
    pic:string
    checked:boolean
    num:number
}
export const useShopcartStore=defineStore("shopcart",{
    state(){
        return{
            shopcartList:[] as Array<ShopcartItem>
        }
    },
    actions:{
       async GET_SHOPCART(){
            const result=await axios.get('http://www.zhaijizhe.cn:3006/shopcart/getShopcartData')
            console.log('result',result.data);
            this.shopcartList=result.data
            
        },
        async CHANGE_NUM(id:number,num:number){  
           const result=await axios.post('http://www.zhaijizhe.cn:3006/shopcart/changeNum',{
                id,num
            })
            if(result.data.code==200){
               this.GET_SHOPCART()
                
            }
            
        }
    },
    getters:{
        totalPrice(state){
            return state.shopcartList.filter(item=>item.checked).reduce((prev,cur)=>prev+cur.price*cur.num,0)
        }
    }
})